const router = require('koa-router')()

router


// 文章类型 ↓

// 添加文章类型
.post('/addArticleType', async (ctx, next) => {
  let query = ctx.request.body
  console.log(query)
  try {
    let articleTypes = await ctx.db.ArticleTypes.create({
      articleTypeName: query.articleTypeName,
      articleTypeDescription: query.articleTypeDescription,
      articleTypeIcon: query.articleTypeIcon,
      createTime: new Date,
      updateTime: new Date
    })
    ctx.body = {
      code: 0,
      message: '添加文章类型成功',
      data: null
    }
  } catch (error) {
    console.log(error.message)
    ctx.body = {
      code: 1,
      message: error.message,
      data: null
    }
  }
})

// 获取文章类型列表 ↓
.get('/getArticleTypeList', async (ctx, next) => {
  let articleTypesList
  try {
    articleTypesList = await ctx.db.ArticleTypes.find()
    ctx.body = {
      code: 0,
      message: '获取列表成功',
      data: articleTypesList
    }
  } catch (error) {
    console.log(error.message)
    ctx.body = {
      code: 0,
      message: error.message,
      data: null
    }
  }
})

// 删除文章类型 ↓
.get('/deleteArticleType', async (ctx, next) => {
  let query = ctx.query
  try {
    let product = await ctx.db.ArticleTypes.deleteOne({
      _id: query.id
    })

    // 删除文章类型的同时，根据被删除ID的来更新文章的articleType
    ctx.db.Articles.update({_id: query.id}, {
      articleType: '5e5264fa2d3723139886ac7d' 
    },{multi: true}) // multi为true设置更新多个文档
    ctx.body = {
      code: 0,
      message: '文章类型删除成功',
      data: null
    }
  } catch (error) {
    console.log(error.message)
    ctx.body = {
      code: 1,
      message: error.message,
      data: null
    }
  }
})

// 更新文章类型 ↓
.post('/updateArticleType', async (ctx, next) => {
  let query = ctx.request.body
  try {
    let articleTypes = await ctx.db.ArticleTypes.findByIdAndUpdate(query.id, {
      articleTypeName: query.articleTypeName,
      articleTypeDescription: query.articleTypeDescription,
      articleTypeIcon: query.articleTypeIcon,
      updateTime: new Date
    })
    ctx.body = {
      code: 0,
      message: '修改文章类型成功',
      data: null
    }
  } catch (error) {
    console.log(error.message)
    ctx.body = {
      code: 1,
      message: error.message,
      data: null
    }
  }
})



// 文章评论 ↓

// 获取文章评论列表
.post('/getCommentList', async (ctx, next) => {
  let query = ctx.request.body
  console.log('获取文章评论', query)
  try {
    let result = await ctx.db.ArticleComments.find({
      commentArticleId: query.commentArticleId
    })
    .skip((query.currentPage-1) * query.pageSize)
    .limit(query.pageSize)
    .sort('-createTime')

    let commentNum = await ctx.db.ArticleComments.find({
      commentArticleId: query.commentArticleId
    }).countDocuments().exec()

    ctx.body = {
      code: 0,
      message: '获取留言成功',
      data: {
        commentList: result,
        counts: commentNum
      } 
    }
  } catch (error) {
    console.log(error.message)
    ctx.body = { code: 1, message: error.message, data: null }
  }
})

// 回复文章评论
.post('/updateCommentReply', async (ctx, next) => {
  let query = ctx.request.body
  console.log(query)
  try {
    let result = await ctx.db.ArticleComments.updateOne({
      _id: query._id
    }, {
      reply: query.reply,
      isReply: true
    })
    ctx.body = { code: 0, message: '文章评论修改成功', data: result }
  } catch (error) {
    console.log(error.message)
    ctx.body = { code: 1, message: error.message, data: null }
  }
})

// 删除文章评论
.post('/deleteComment', async (ctx, next) => {
  let query = ctx.request.body
  try {
    let result = await ctx.db.ArticleComments.deleteOne({ _id: query._id})
    ctx.body = { code: 0, message: '删除评论成功', data: result }
    let commentNum = await ctx.db.ArticleComments.find({
      commentArticleId: query.commentArticleId
    }).countDocuments().exec()
    ctx.db.Articles.updateOne(
      {_id: query.commentArticleId}, 
      { 'commentNum': commentNum }
    )
    .then(res => {
      console.log('修改评论数成功', res)
    })
  } catch (error) {
    console.log(error.message)
    ctx.body = { code: 1, message: error.message, data: null }
  }
})



// 文章

// 添加文章 ↓
.post('/addArticle', async (ctx, next) => {
  let query = ctx.request.body
  try {
    let product = await ctx.db.Articles.create({
      ...query,
      createTime: new Date()
    })
    console.log(product)
    ctx.body = {
      code: 0,
      message: '添加成功',
      data: product
    }
  } catch (error) {
    console.error(error.message)
    ctx.body = {
      code: 1,
      message: error.message,
      data: null
    }
  }
  
})

// 获取文章列表 ↓
.post('/getArticleList', async (ctx, next) => {

  /*
    入参
    title: String,
    type: id,
    date: Date,
    pageSize: Number,
    currentPage: Number,
    sort: String,
    isRecommend: Boolean,
    isHome: Boolean,
  */
  let query = ctx.request.body
  let sort = query.sort.toString()
  console.log(query)

  // 设置查询对象
  let selectOption = {}
  if(!!query.date) selectOption['createTime'] = {$gt:query.date[0],$lt: query.date[1]}
  if(!!query.type) selectOption['articleType'] = query.type
  if(!!query.title) selectOption['articleTitle'] =  {$regex: query.title}
  if(!!query.isRecommend) selectOption['isRecommend'] = true
  if(!!query.isHome) selectOption['isHome'] = true

  try {

    const product = await ctx.db.Articles.find({
      ...selectOption
    })
    .populate('articleType')
    .sort('-' + query.sort)
    .skip((query.currentPage-1) * query.pageSize)
    .limit(query.pageSize)
    .exec()

    let counts = await ctx.db.Articles.find({
      ...selectOption
    }).countDocuments().exec()

    ctx.body = {
      code: 0,
      message: '获取文章列表成功',
      data: {
        articleList: product,
        counts: counts
      }
    }
  } catch (error) {
    console.log(error.message)
    ctx.body = { code: 1, message: error.message, data: null }
  }
})

// 查找文章 ↓
.get('/selectArticleById', async (ctx, next) => {
  let parmas = ctx.query
  console.log(parmas.id)
  try {
    let result = await ctx.db.Articles.findOne({_id: parmas.id})
    ctx.body = {
      code: 0,
      message: '查询成功',
      data: result
    }
  } catch (error) {
    console.log(error.message)
    ctx.body = {
      code: 1,
      message: error.message,
      data: null
    }
  }
})

// 删除文章
.get('/deleteArticleById', async (ctx, next) => {
  let parmas = ctx.query
  try {
    let result = await ctx.db.Articles.deleteOne({_id: parmas.id})
    ctx.body = {
      code: 0,
      message: '删除成功',
      data: result
    }
  } catch (error) {
    console.log(error.message)
    ctx.body = {
      code: 1,
      message: error.message,
      data: null
    }
  }
})

// 更新文章 ↓
.post('/updateArticleById', async (ctx, next) => {
  let query = ctx.request.body
  try {
    let article = await ctx.db.Articles.findByIdAndUpdate(query.id, {
      articleTitle: query.articleTitle,
      articleDescription: query.articleDescription,
      articleContent: query.articleContent,
      articleAuthor: query.articleAuthor,
      articleType: query.articleType,
      updateTime: new Date()
    })
    console.log(article)
    ctx.body = {
      code: 0,
      message: '修改文章成功',
      data: null
    }
  } catch (error) {
    console.log(error.message)
    ctx.body = { code: 1, message: error.message, data: null }
  }
})

// 设置是否显示
.post('/setArticle', async (ctx, next) => {
  let query = ctx.request.body
  console.log(query)
  try {
    let article = await ctx.db.Articles.findByIdAndUpdate(query.id, {
      ...query.data,
      updateTime: new Date(),
    })
    ctx.body = { code: 0, message: '设置推荐成功', data: article }
  } catch (error) {
    console.log(error.message)
    ctx.body = { code: 1, message: error.message, data: null }
  }
})


module.exports = router